//单位菜单及生成指令

//zorder : 12
var directionInfo = {
	encounterImage : [resource.troopThrustAttack_png, resource.troopStopAndAttack_png],

	battleImage : [resource.troopPositive_png, resource.troopPassive_png],
	
	trivialImage : [resource.troopPassDown_png,	resource.troopPassDownMeantime_png,
	               resource.troopPassUp_png,	resource.troopPassUpMeantime_png,
	               resource.troopDefendDown_png,resource.troopDefendDownMeantime_png,
	               resource.troopDefendUp_png,	resource.troopDefendUpMeantime_png,
	               resource.troopRest_png,		resource.troopRestMeantime_png],
	
	troopRouteNode : [],
	
	marchSprites : [],
	battleSprites : [],
	trivialSprites : [],
	
	_folded : [],
	
	_unfoldScale : 1.0,
	unfoldLocation : [],
	
	_foldScale : 0.4,
	foldLocation : [],
	
	Initialize : function(){
		this.troopRouteNode = [];
		this.marchSprites = [];
		this.battleSprites = [];
		this.trivialSprites = [];
		this._folded = [];
		
		var hDiv = 0.5 * tileEngine.tileHeight;
		var wDiv = 0.5 * tileEngine.tileWidth;
		this.foldLocation = [
		    {x : -1.7 * this._foldScale * wDiv, y : -0.75 * hDiv},
            {x : 0,                             y : -0.75 * hDiv},
            {x : 1.7 * this._foldScale * wDiv, y : -0.75 * hDiv}
		];
		this.unfoldLocation = [
            {x : -2 * this._unfoldScale * wDiv, y : -0.75 * hDiv},
            {x : 0,                             y : -0.75 * hDiv},
            {x : 2 * this._unfoldScale * wDiv, y : -0.75 * hDiv}
		];
		
		for (var troopid = 0; troopid < campaign.troop.length; troopid++){
			this.troopRouteNode[troopid] = new cc.Node();
			mainMap.node.addChild(this.troopRouteNode[troopid], 9);
		}
	},
	
	DrawTroopRoute : function(troopid, route, arrowIdxs){
		this.ClearTroopRoute(troopid);
		
		if (route.length < 2 || arrowIdxs.length < 2)
			return;

		var foreColor = campaign.troop[troopid].color;
		
		for (var idx = 1; idx < arrowIdxs.length; idx++){
			//Route Points
			var points = [];
			var jdx = 0;
			for (var kdx = arrowIdxs[idx - 1]; kdx <= arrowIdxs[idx]; kdx++){
				var coord = tileEngine.IndexToCoord(route[kdx]);
				points[jdx++] = cc.p(coord.x, coord.y);
			}
			
			//Draw Arrow
			if (jdx < 2)
				continue;
			jdx--;
			
			var troopRouteArrow = new cc.Sprite(resource.arrow_png);
			troopRouteArrow.attr({
				scale : 1.5,
				color : foreColor
			});
			troopRouteArrow.setPosition(
				0.8 * points[jdx].x + 0.2 * points[jdx - 1].x,
				0.8 * points[jdx].y + 0.2 * points[jdx - 1].y);
			troopRouteArrow.setRotation(TwoPointHeading(points[jdx - 1], points[jdx]));
			this.troopRouteNode[troopid].addChild(troopRouteArrow);
			
			//Draw Route
			points[0].x = 0.8 * points[0].x + 0.2 * points[1].x;
			points[0].y = 0.8 * points[0].y + 0.2 * points[1].y;
			points[jdx].x = 0.8 * points[jdx].x + 0.2 * points[jdx - 1].x;
			points[jdx].y = 0.8 * points[jdx].y + 0.2 * points[jdx - 1].y;

			var trooopRouteDrawer = new cc.DrawNode();
			
			trooopRouteDrawer.drawCardinalSpline(
				points,
				0.1, 4 * points.length,
				20,
				cc.color(foreColor.r,foreColor.g,foreColor.b,191));
			this.troopRouteNode[troopid].addChild(trooopRouteDrawer);
		}
	},
	
	DrawTroopCourtRoute : function(troopid, route, arrowIdxs){
		this.ClearTroopRoute(troopid);
		
		if (route.length < 2 || arrowIdxs.length < 2)
			return;
		
		var foreColor = campaign.troop[troopid].color;
		
		var tileIndex = route[arrowIdxs[1]];
		for (var neighborid in tileEngine.neighborCache[tileIndex]){
			var tgtTileIndex = tileEngine.neighborCache[tileIndex][neighborid];
			
			var bomb = new cc.Sprite(resource.bomb_png);
			bomb.attr(tileEngine.IndexToCoord(tgtTileIndex));
			bomb.setColor(cc.color(foreColor.r,foreColor.g,foreColor.b,191));
			this.troopRouteNode[troopid].addChild(bomb);
		}
		var bomb = new cc.Sprite(resource.bomb_png);
		bomb.attr(tileEngine.IndexToCoord(tileIndex));
		bomb.setColor(cc.color(foreColor.r,foreColor.g,foreColor.b,191));
		this.troopRouteNode[troopid].addChild(bomb);
	},

	ClearTroopRoute : function(troopid){
		if (this.troopRouteNode[troopid].getChildrenCount() > 0){
			this.troopRouteNode[troopid].removeAllChildren();
		}
	},
	
	BlinkTroopRoute : function(troopid){
		this.troopRouteNode[troopid].runAction(cc.blink(0.4, 3));
	},
	
	AddTroopDirection : function(troopid, direction, isFold){
		this.ClearTroopDirection(troopid);
		
		this._folded[troopid] = isFold;
		
		if (warTroop.troopHolders == null || warTroop.troopHolders[troopid] == null)
			return;
		
		var coord = tileEngine.IndexToCoord(campaign.troop[troopid].tileIndex);
		var sc = isFold ? this._foldScale : this._unfoldScale;
		var loc = isFold ? this.foldLocation : this.unfoldLocation;
		
		this.marchSprites[troopid] = this._CreateBackgroundSprite(this.encounterImage[direction.encounter]);
		this.marchSprites[troopid].attr(loc[0]);
		this.marchSprites[troopid].setScale(sc);
		warTroop.troopHolders[troopid].addChild(this.marchSprites[troopid], 1);
		
		this.battleSprites[troopid] = this._CreateBackgroundSprite(this.battleImage[direction.battle]);
		this.battleSprites[troopid].attr(loc[1]);
		this.battleSprites[troopid].setScale(sc);
		warTroop.troopHolders[troopid].addChild(this.battleSprites[troopid], 1);
		
		this.trivialSprites[troopid] = this._CreateBackgroundSprite(this.trivialImage[direction.trivial * 2 + direction.trivialMeantime]);
		this.trivialSprites[troopid].attr(loc[2]);
		this.trivialSprites[troopid].setScale(sc);
		warTroop.troopHolders[troopid].addChild(this.trivialSprites[troopid], 1);
		
	},
	
	_CreateBackgroundSprite : function(spriteImage){
		var sprite = new cc.Sprite(spriteImage);
		var backPicture = new cc.Sprite(resource.troopBtnNormal_png);
		backPicture.attr({x : 0.5 * sprite.width, y : 0.5 * sprite.height});
		sprite.addChild(backPicture, -1);
		return sprite;
	},
	
	ClearTroopDirection : function(troopid){
		if (this.marchSprites[troopid] != null){
			this.marchSprites[troopid].removeFromParent();
			this.marchSprites[troopid] = null;
		}
		if (this.battleSprites[troopid] != null){
			this.battleSprites[troopid].removeFromParent();
			this.battleSprites[troopid] = null;
		}
		if (this.trivialSprites[troopid] != null){
			this.trivialSprites[troopid].removeFromParent();
			this.trivialSprites[troopid] = null;
		}
	},
	
	foldDuration : 0.2,
	
	FoldTroopDirection : function(troopid, isFold){
		if (this._folded[troopid] == isFold)
			return;
		this._folded[troopid] = isFold;
		
		var sc = isFold ? this._foldScale : this._unfoldScale;
		var loc = isFold ? this.foldLocation : this.unfoldLocation;
		
		if (this.marchSprites[troopid] != null){
			var scaleTo = cc.scaleTo(this.foldDuration, sc);
			var moveTo = cc.moveTo(this.foldDuration, loc[0].x, loc[0].y);
			this.marchSprites[troopid].runAction(scaleTo);
			this.marchSprites[troopid].runAction(moveTo);
		}
		if (this.battleSprites[troopid] != null){
			var scaleTo = cc.scaleTo(this.foldDuration, sc);
			var moveTo = cc.moveTo(this.foldDuration, loc[1].x, loc[1].y);
			this.battleSprites[troopid].runAction(scaleTo);
			this.battleSprites[troopid].runAction(moveTo);
		}
		if (this.trivialSprites[troopid] != null){
			var scaleTo = cc.scaleTo(this.foldDuration, sc);
			var moveTo = cc.moveTo(this.foldDuration, loc[2].x, loc[2].y);
			this.trivialSprites[troopid].runAction(scaleTo);
			this.trivialSprites[troopid].runAction(moveTo);
		}
	},
};